﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace InformationExtraction
{
    class Page
    {
        public string Title { get; private set; }        
        public string RawContent { get; private set; }        
        public string PlainText
        {
            get
            {
                if (_plainText == null)
                {
                    _plainText = Extractor.StripPos(RawContent);
                }
                return _plainText;
            }
        }
        public List<Sentence> Sentences
        {
            get
            {
                if (_sentences == null)
                {
                    //dotPattern: ./.
                    Regex dotPattern = new Regex(@"\./\.");
                    string[] sents = dotPattern.Split(RawContent);
                    _sentences = new List<Sentence>(sents.Length);
                    foreach (string str in sents)
                        if (!string.IsNullOrWhiteSpace(str))
                            _sentences.Add(new Sentence(str.Trim() + "."));
                }
                return _sentences;
            }
        }

        private string _plainText = null;
        private List<Sentence> _sentences = null;

        public Page(string title, string content)
        {
            this.Title = title;
            this.RawContent = content;
        }

        public int GetRawSentenceIndex(int indexInPage)
        {
            int sum = 0;
            
            for (int i = 0; i < Sentences.Count; i++)
            {
                int length = Sentences[i].RawSentence.Length;
                if (sum + length > indexInPage)
                    return i;
                sum += length - 1;
            }

            return -1;
        }
    }
}
